package com.qf.lanzhou.blog.api.controller.PageBreak;

import com.qf.lanzhou.blog.api.controller.BaseServletPost;
import com.qf.lanzhou.blog.api.pojo.entity.Article;
import com.qf.lanzhou.blog.api.pojo.entity.Auxiliary;
import com.qf.lanzhou.blog.api.pojo.entity.Page;
import com.qf.lanzhou.blog.api.service.ArtivleTypeService;
import com.qf.lanzhou.blog.api.service.impl.ArtivleTypeServicelmpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author jack
 * 分类分页
 */
@WebServlet("/articleTypeBreak.do")
public class ArticleTypeBreak extends BaseServletPost {
    private ArtivleTypeService artivleTypeService =  new ArtivleTypeServicelmpl();
    private static Long type;
    private static Integer numberPage;
    private Page page = new Page(1);
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        System.out.println("分类分页服务器连接");
        Auxiliary auxiliary = paramsToEntity(req, Auxiliary.class);
        System.out.println(auxiliary);
        //判断类型是否为空 类型不为空，跳转到该类型首页 首页为空，正常跳转
        if (auxiliary.getType()!=null){
            type = Long.decode(auxiliary.getType());
            //跳转到第一页
            numberPage = 1;
            success(resp);
            //发送成功
            return;
        }
        //获取到类型的最大页码数
        Integer typeCount = artivleTypeService.getTypeCount(type);
        //赋给page
        page.setTotalCounts(typeCount);
        //尾页
        if (auxiliary.getMax()!=null){
            numberPage=page.getTotalPage();
            success(resp);
            return;
        }
        //首页
        if (auxiliary.getFist()!=null){
            numberPage=1;
            success(resp);
            return;
        }
        //页码跳转
        if (auxiliary.getB()!=null){
            Integer b = Integer.decode(auxiliary.getB());
            //判断是否超出或者小于最小页码
            if (b<=0 || b>page.getTotalPage()){
                String msg = "超出或者小于页码";
                failedResultWrite(msg,resp);
                return;
            }
            //正常跳转
            numberPage = b;
            success(resp);
            return;
        }
        //前进后退

        if (auxiliary.getA()==null){
            //前进
            if (numberPage == page.getTotalPage()){
                String msg = "已经是最后一页了哦";
                failedResultWrite(msg,resp);
                return;
            }
            numberPage++;
            success(resp);
            return;
        }
        if (auxiliary.getA()!=null){
            //后退
            if (numberPage==1){
                String msg = "这是第一页哦";
                failedResultWrite(msg,resp);
                return;
            }
            numberPage--;
            success(resp);
            return;
        }
    }

    private void success(HttpServletResponse resp) {
        List<Article> list = artivleTypeService.selectPageByType(type, numberPage);
        List dataList = getDataList(list);
        successResultWrite(dataList, resp);
    }


    private List getDataList(List list){
        for (Object article:list) {
            Article article1=(Article)article;
            article1.setArticleProduce(article1.getArticleCreatetime());
        }
        return list;
    }
}
